package com.gxa.controller.community;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.common.utils.Result;
import com.gxa.dto.communitydto.UserApplyDto;
import com.gxa.dto.communitydto.UserApplySearchDto;
import com.gxa.entity.community.House;
import com.gxa.entity.community.UserApply;
import com.gxa.service.community.UserApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-11 10:05:39
 */
@Api(tags = "申请用户的接口")
@RestController
@RequestMapping("/userApply")
public class UserApplyController {

    @Autowired
    private UserApplyService userApplyService;

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "申请用户列表接口")
    @GetMapping("/list")
    public Result<PageInfo<UserApply>> queryAll(UserApplySearchDto userApplySearchDto,Integer pageNum,Integer pageSize){
        PageInfo<UserApply> userApplyPageInfo;
        try{
//        System.out.println(userApplySearchDto.getStartTime());
            userApplyPageInfo = userApplyService.queryAll(userApplySearchDto, pageNum, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查询失败");
        }
        return Result.success(userApplyPageInfo);
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "新增申请用户接口")
    @PostMapping("/add01")
    public Result add01(@RequestBody UserApply userApply){
        try{
            userApplyService.add(userApply);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }
        return Result.success();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "删除申请用户接口")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Integer id){
        try{
        userApplyService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("删除失败");
        }

        return Result.success();
    }
//    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
//    @ApiOperation(value = "根据id查询申请用户接口")
//    @GetMapping("/queryById/{id}")
//    public R queryById(@PathVariable("id") Integer id){
//        for(UserApply d:list){
//            if (id == d.getId()){
//                return ok("queryById").put("this",d);
//            }
//        }
//        return error("query error");
//    }
//    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
//    @ApiOperation(value = "编辑申请用户接口")
//    @PutMapping("/edit")
//    public R edit(@RequestBody UserApply userApply){
//        for(UserApply d:list){
//            if(d.getId()== userApply.getId()){
//                d = userApply;
//                return R.ok().put("this",userApply);
//            }
//        }
//        return ok("edit error");
//    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation(value = "用户审核接口")
    @PutMapping("/editStatus")
    public Result editStatus(@RequestBody UserApply userApply){

        try{
            userApplyService.updateState(userApply);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("修改失败");
        }
        return Result.success();
    }
}
